package com.unity3d.player;

import android.os.*;

import java.io.IOException;
import java.security.*;
import java.net.*;

import javax.net.ssl.*;

public final class a extends SSLSocketFactory {
    private static volatile SSLSocketFactory c;
    private static final Object[] d;
    private static final boolean e;
    private final SSLSocketFactory sslSocketFactory;
    private final SimpleSSLServer b;

    static {
        final boolean b = false;
        d = new Object[0];
        boolean e2 = b;
        if (Build.VERSION.SDK_INT >= 16) {
            e2 = b;
            if (Build.VERSION.SDK_INT < 20) {
                e2 = true;
            }
        }
        e = e2;
    }

    private a() throws NoSuchAlgorithmException, KeyManagementException {
        final SSLContext instance = SSLContext.getInstance("TLS");
        instance.init(null, null, null);
        this.sslSocketFactory = instance.getSocketFactory();
        this.b = new SimpleSSLServer();
    }

    private static Socket a(Socket socket) {
        if (socket != null && socket instanceof SSLSocket && a.e) {
            final SSLSocket sslSocket = (SSLSocket) socket;
            sslSocket.setEnabledProtocols(sslSocket.getSupportedProtocols());
        }
        return socket;
    }

    public static SSLSocketFactory a() {
        synchronized (a.d) {
            if (a.c != null) {
                return a.c;
            }
            try {
                return a.c = new a();
            } catch (Exception ex) {
                final StringBuilder sb = new StringBuilder("CustomSSLSocketFactory: Failed to create SSLSocketFactory (");
                sb.append(ex.getMessage());
                sb.append(")");
                com.unity3d.player.e.Log(5, sb.toString());
                return null;
            }
        }
    }

    @Override
    public final Socket createSocket() throws IOException {
        return a(this.sslSocketFactory.createSocket());
    }

    @Override
    public final Socket createSocket(final String s, final int n) throws IOException {
        return a(this.sslSocketFactory.createSocket(s, n));
    }

    @Override
    public final Socket createSocket(final String s, final int n, final InetAddress inetAddress, final int n2) throws IOException {
        return a(this.sslSocketFactory.createSocket(s, n, inetAddress, n2));
    }

    @Override
    public final Socket createSocket(final InetAddress inetAddress, final int n) throws IOException {
        return a(this.sslSocketFactory.createSocket(inetAddress, n));
    }

    @Override
    public final Socket createSocket(final InetAddress inetAddress, final int n, final InetAddress inetAddress2, final int n2) throws IOException {
        return a(this.sslSocketFactory.createSocket(inetAddress, n, inetAddress2, n2));
    }

    @Override
    public final Socket createSocket(final Socket socket, final String s, final int n, final boolean b) throws IOException {
        return a(this.sslSocketFactory.createSocket(socket, s, n, b));
    }

    @Override
    public final String[] getDefaultCipherSuites() {
        return this.sslSocketFactory.getDefaultCipherSuites();
    }

    @Override
    public final String[] getSupportedCipherSuites() {
        return this.sslSocketFactory.getSupportedCipherSuites();
    }

    final class SimpleSSLServer implements HandshakeCompletedListener {
        @Override
        public final void handshakeCompleted(final HandshakeCompletedEvent handshakeCompletedEvent) {
            final SSLSession session = handshakeCompletedEvent.getSession();
            session.getCipherSuite();
            session.getProtocol();
            try {
                session.getPeerPrincipal().getName();
            } catch (SSLPeerUnverifiedException ex) {
                ex.printStackTrace();
            }
        }
    }
}
